<?php
namespace app\cms\service\jd;

use app\cms\service\entities\Good;
use app\cms\service\jd\stub\GoodsResp;
use app\cms\service\SalesPlatformInterface;

class JosPlatform implements SalesPlatformInterface
{

    private JdClientWrapper $client;

    public function __construct(JdClientWrapper $client){
        $this->client = $client;
    }

    /**
     * @param String $keyword
     * @param int $page
     * @param int $pageSize
     * @return array|Good[]
     */
    public function queryGoods(string $keyword, int $page, int $pageSize): array
    {
        $result = $this->client->queryByKeyword($keyword, $page, $pageSize);

        $goods = [];
        foreach ($result as $goodRes) {

            $materialUrl = $goodRes->materialUrl;
            //TODO 这里先使用第一个优惠券。
            $couponUrl = $goodRes->couponInfo->couponList[0]->link;

            $good = new Good();
            $good->clickUrl = $this->client->convertClickUrl($materialUrl, $couponUrl);
            $good->iid = $goodRes->skuId;
            $good->platform = '京东';
            $good->rawPrice = $goodRes->priceInfo->price;
            $good->promPrice = $goodRes->priceInfo->lowestCouponPrice;
            $good->title = $goodRes->skuName;
            $good->picUrl = empty($goodRes->imageInfo->imageList) ?
                $goodRes->imageInfo->whiteImage
                :
                $goodRes->imageInfo->imageList[0]->url;

            $goods[] = $good;
        }
        return $goods;
    }

}